require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'net/http'
require 'uri'

#プロフィールから画像ファイルをダウンロード
def parseProfile(url)
	proDoc = Nokogiri::HTML(open(url))
  savepath = 'C:\Temp\\'
	
	image_url_list = []
	
	#名前
	proDoc.css('dl.profile.clearfix dt img').each do |img|
		image_url_list.push img['src']
	end
	

	
  # get image file
  image_url_list.each do |image_url|
  	puts image_url
    filename = File.basename(image_url)
    open(savepath + filename, 'wb') do |output|
      output.puts Net::HTTP.get_response(URI.parse(image_url)).body
      #open(image_url) do |data|
      #  output.write(data.read)
      #end
      puts("#{filename} has saved.")
    end
  end

	
	
end



def main
	root_url = 'http://www.ske48.co.jp/profile/list.php'
	base_url = 'http://www.ske48.co.jp/profile/'

	# Get a Nokogiri::HTML:Document for the page we’re interested in...
	doc = Nokogiri::HTML(open(root_url))

	doc.css('ul.list.clearfix li dl dd h3 a').each do |link|
	  parseProfile( base_url + link['href'])
	  sleep(1)
	  
	end
end

main

__END__

# Do funky things with it using Nokogiri::XML::Node methods...

####
# Search for nodes by css
#doc.css('h3.r a.l').each do |link|
#puts link.content
#end
